package com.tutu.user.security;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.springframework.security.access.SecurityMetadataSource;
import org.springframework.security.access.intercept.AbstractSecurityInterceptor;
import org.springframework.security.access.intercept.InterceptorStatusToken;
import org.springframework.security.web.FilterInvocation;
import org.springframework.security.web.access.intercept.FilterInvocationSecurityMetadataSource;

/**
 * 	@author tutu
 *	@date 2013-11-19
 *	@company cjcc
 *	@version ssi_1.0 1.0
 */
public class MyFilterSecurityInterceptor extends AbstractSecurityInterceptor implements Filter{

	@Resource(name="mySecurityMetadataSource")
	private FilterInvocationSecurityMetadataSource securityMetadataSource;
	
	@Override
	public void destroy() {
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
        FilterInvocation filterInvocation = new FilterInvocation(request, response, chain);  
        InterceptorStatusToken token = super.beforeInvocation(filterInvocation);  
        try {  
        	filterInvocation.getChain().doFilter(filterInvocation.getRequest(), filterInvocation.getResponse());  
//        } catch (Exception e) {  
//            this.logger.error("用户权限验证跳转失败", e);
        } finally {
            super.afterInvocation(token, null);  
        }
	}

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
	}

	@Override
	public Class<?> getSecureObjectClass() {
		return FilterInvocation.class;
	}

	@Override
	public SecurityMetadataSource obtainSecurityMetadataSource() {
		return this.securityMetadataSource;
	}

	public void setSecurityMetadataSource(
			FilterInvocationSecurityMetadataSource securityMetadataSource) {
		this.securityMetadataSource = securityMetadataSource;
	}

}
